package com.cornergj.back.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cornergj.chat.webstock.MyWebSocketOtO;
import com.cornergj.model.Admin;
import com.cornergj.model.AuditApplication;
import com.cornergj.model.Entity;
import com.cornergj.model.Entity.AdminCriteria;
import com.cornergj.model.Entity.AuditApplicationCriteria;
import com.cornergj.model.Entity.Pagination;
import com.cornergj.model.Entity.UserBasicInfoCriteria;
import com.cornergj.model.Entity.Value;
import com.cornergj.model.Nanny;
import com.cornergj.model.SiteOfCity;
import com.cornergj.model.SiteOfProvince;
import com.cornergj.model.UserBasicInfo;
import com.cornergj.model.WorkType;
import com.cornergj.response.ErrorCode;
import com.cornergj.response.JsonResult;
import com.cornergj.service.AdminService;
import com.cornergj.service.AuditApplicationService;
import com.cornergj.service.NannyService;
import com.cornergj.service.SiteOfCityService;
import com.cornergj.service.SiteOfProvinceService;
import com.cornergj.service.UserBasicInfoService;
import com.cornergj.service.WorkTypeService;
import com.cornergj.uitl.CryptUtil;
import com.cornergj.uitl.FileUtils;


/**
 * Title:
 * Model:
 * Description:
 * @author 郑耿杰
 * @date 2018年1月31日
 */
@Controller
@RequestMapping("/nannyappli")
public class NannyApplicationController {
	
	@Autowired
	private AuditApplicationService auditApplicationService;
	@Autowired
	private WorkTypeService workTypeService;
	@Autowired
	private SiteOfProvinceService siteOfProvinceService;
	@Autowired
	private SiteOfCityService siteOfCityService;
	@Autowired
	private NannyService nannyService;
	/**
	 * 分页方法
	 * @param request
	 * @param bean
	 * @param pagination
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(HttpServletRequest request, AuditApplication bean, Pagination pagination) {
		//查询条件封装
		AuditApplicationCriteria criteria = new AuditApplicationCriteria();
		criteria.setPass(Value.eq(0));
		//分页查询
		Pagination list = auditApplicationService.selectPage(criteria, pagination);
		request.setAttribute("pagination", list);
		request.setAttribute("bean", bean);
		return "nannyapplication/list";
	}
	/**
	 * 查看方法
	 * @param request
	 * @param bean
	 * @param pagination
	 * @return
	 */
	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public String view(HttpServletRequest request,int aid, Pagination pagination) {
		AuditApplication auditApplication = auditApplicationService.selectOne(aid);
		WorkType workType=workTypeService.selectOne(auditApplication.getWorkTypeId());
		SiteOfProvince province=siteOfProvinceService.selectOne(auditApplication.getServiceProvince());
		SiteOfCity city=siteOfCityService.selectOne(auditApplication.getServiceCity());
		request.setAttribute("province", province);
		request.setAttribute("city", city);
		request.setAttribute("workType", workType);
		request.setAttribute("auditApplication", auditApplication);
		return "nannyapplication/view";
	}
	/*
	 * 同意
	 */
	@RequestMapping(value = "/oper", method = RequestMethod.GET)
	public @ResponseBody JsonResult oper(HttpServletRequest request,int aid,int status,HttpServletResponse response) {
		JsonResult result = new JsonResult();
		AuditApplication auditApplication = auditApplicationService.selectOne(aid);
		try{
			if(status==1){
				Nanny nanny=new Nanny();
				nanny.setAddress(auditApplication.getAddress());
				nanny.setIdentityImgUrl(auditApplication.getIdentityImgUrl());
				nanny.setIdentityImgKey(auditApplication.getIdentityImgKey());
				nanny.setIdNumber(auditApplication.getIdNumber());
				nanny.setPostCode(auditApplication.getPostCode());
				nanny.setPhone(auditApplication.getPhone());
				nanny.setProfile(auditApplication.getProfile());
				nanny.setTruename(auditApplication.getTruename());
				nanny.setServiceProvince(auditApplication.getServiceProvince());
				nanny.setServiceCity(auditApplication.getServiceCity());
				nanny.setWorkTypeId(auditApplication.getWorkTypeId());
				nannyService.save(nanny);
				auditApplicationService.delete(aid);
			}else{
				auditApplication.setPass(status);
				auditApplicationService.update(auditApplication);
			}
		}catch(Exception e){
			e.printStackTrace();
			result.addErrorCode(ErrorCode.SYS_ERR);
		}
		return result;
	}
	/*
	 * 忽略/删除
	 */
	@RequestMapping(value = "/reject", method = RequestMethod.GET)
	public @ResponseBody JsonResult reject(HttpServletRequest request,int aid,HttpServletResponse response) {
		JsonResult result = new JsonResult();
		try{
			AuditApplication auditApplication = auditApplicationService.selectOne(aid);
			if(FileUtils.deleteFile(auditApplication.getIdentityImgKey())){
				auditApplicationService.delete(aid);
			}else{
				result.addErrorCode(ErrorCode.SYS_ERR);
			}
		}catch(Exception e){
			e.printStackTrace();
			result.addErrorCode(ErrorCode.SYS_ERR);
		}
		return result;
	}
}
